package com.ztgf.order.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.order.mapper.OrderGMVStatisticsMapper;
import com.ztgf.order.mapper.OrderSalesStatisticsMapper;
import com.ztgf.order.models.bo.OrderGMVStatisticsBO;
import com.ztgf.order.models.bo.OrderShopStatisticsBO;
import com.ztgf.order.models.bo.OrderStatisticsBO;
import com.ztgf.order.models.service.IOrderGMVStatisticsService;
import com.ztgf.order.models.vo.OrderGMVStatisticsVO;
import com.ztgf.order.models.vo.OrderSalesCategoryNewVO;
import com.ztgf.order.models.vo.OrderSalesCategoryShopVO;
import com.ztgf.order.models.vo.OrderSalesCategoryVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.TWO;
import static com.ztgf.common.utils.UtilConstants.isNull;

/*
 * @author: chenyu.wang
 * @date: 2020-08-10
 * @description:后台管理系统GMV统计服务
 */
@DubboService
public class OrderGMVStatisticsServiceImpl implements IOrderGMVStatisticsService {

    @Autowired
    private OrderGMVStatisticsMapper orderGMVStatisticsMapper;
    @Autowired
    private OrderSalesStatisticsMapper orderSalesStatisticsMapper;

    @Override
    public OrderGMVStatisticsVO queryOrderStateAmount(OrderGMVStatisticsBO bo) {
        OrderGMVStatisticsVO vo = new OrderGMVStatisticsVO();
        vo.setCompleted(orderGMVStatisticsMapper.getOrderStateAmount(5, bo));
        vo.setPendingPayment(orderGMVStatisticsMapper.getOrderStateAmount(2, bo));
        vo.setToBeDelivered(orderGMVStatisticsMapper.getOrderStateAmount(3, bo));
        vo.setToBeSigned(orderGMVStatisticsMapper.getOrderStateAmount(4, bo));
        vo.setCancelled(orderGMVStatisticsMapper.getOrderStateAmount(6, bo));
        vo.setTotalOrderAmountToday(orderGMVStatisticsMapper.getTotalAmountOfOrders(bo));
        vo.setToBeDetermined(orderGMVStatisticsMapper.getOrderStateAmount(0, bo));
        vo.setContractToBeSigned(orderGMVStatisticsMapper.getOrderStateAmount(1, bo));
        return vo;
    }

    @Override
    public Page<OrderSalesCategoryVO> queryOrderSalesCategory(OrderStatisticsBO bo) {
        Page<OrderSalesCategoryVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<OrderSalesCategoryVO> list = orderSalesStatisticsMapper.queryAllFianlCategorySales(page);
        if (isNull(list)) {
            return null;
        }
        for (OrderSalesCategoryVO vo : list) {
            String realPrice;
            BigDecimal price = orderGMVStatisticsMapper.queryOrderSalesCategory(vo.getId(), bo);
            realPrice = price.setScale(TWO, BigDecimal.ROUND_HALF_UP).toString();
            vo.setOrderPrice(realPrice);
        }
        list.sort(new Comparator<OrderSalesCategoryVO>() {
            public int compare(OrderSalesCategoryVO o1, OrderSalesCategoryVO o2) {
                if (o1.getOrderPrice().compareTo(o2.getOrderPrice()) > 0) {
                    return -1;
                }
                if (o1.getOrderPrice().equals(o2.getOrderPrice())) {
                    return 0;
                }
                return 1;
            }
        });
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<OrderSalesCategoryShopVO> queryOrderVariousShopItems(OrderShopStatisticsBO bo) {
        Page<OrderSalesCategoryShopVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<OrderSalesCategoryShopVO> list = orderSalesStatisticsMapper.querySalesShopCategory1(bo);
        if (isNull(list)) {
            return page;
        }
        List<OrderSalesCategoryShopVO> voList = orderGMVStatisticsMapper.queryOrderMoney(bo);
        if (voList!=null){
            for (OrderSalesCategoryShopVO vo : list) {
                boolean flag=true;
                for (OrderSalesCategoryShopVO orderSalesCategoryShopVO : voList) {
                    if (vo.getShopId()!=null&&vo.getCategoryId()!=null){
                        if (vo.getShopId().equals(orderSalesCategoryShopVO.getShopId()) && vo.getCategoryId().equals(orderSalesCategoryShopVO.getCategoryId())){
                            vo.setOrderSales(orderSalesCategoryShopVO.getOrderSales());
                            flag=false;
                        }
                    }

                }
                if (flag){
                    vo.setOrderSales(new BigDecimal("0.00").toString());
                }
            }
            for (OrderSalesCategoryShopVO orderSalesCategoryShopVO : voList) {
                if (orderSalesCategoryShopVO.getCategoryId()==null){
                    orderSalesCategoryShopVO.setCategoryName("虚拟商品类目");
                    list.add(orderSalesCategoryShopVO);
                }
            }
        }else {
            for (OrderSalesCategoryShopVO orderSalesCategoryShopVO : list) {
                orderSalesCategoryShopVO.setOrderSales(new BigDecimal("0.00").toString());
            }
        }
        list.sort((OrderSalesCategoryShopVO a,OrderSalesCategoryShopVO b)->Double.valueOf(b.getOrderSales()).compareTo(Double.valueOf(a.getOrderSales())));

        int count=0;
        if(list != null && list.size() > 0) {
            page.setTotal(list.size());
            count = list.size();
            int fromIndex = (bo.getPageNo()-1) * bo.getPageSize();
            int toIndex = bo.getPageNo() * bo.getPageSize();
            if (toIndex > count) {
                toIndex = count;
            }
            List<OrderSalesCategoryShopVO> orderSalesCategoryShopVOS = list.subList(fromIndex, toIndex);
            page.setRecords(orderSalesCategoryShopVOS);

            return page;
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<OrderSalesCategoryNewVO> queryNewGmvOrderCount(OrderStatisticsBO bo) {
        Page<OrderSalesCategoryNewVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<OrderSalesCategoryNewVO> list = orderGMVStatisticsMapper.queryNewGmvOrderCount(page, bo);
        page.setRecords(list);
        return page;
    }
}
